package com.yf.service.im.impl;

import com.yf.dao.im.GroupUserDao;
import com.yf.dao.UserDao;
import com.yf.entity.GroupUserEntity;
import com.yf.entity.UserEntity;
import com.yf.response.RRException;
import com.yf.service.im.FriendService;
import com.yf.service.impl.BaseServiceImpl;
import com.yf.vo.FriendVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.yf.dao.im.FriendDao;
import com.yf.entity.FriendEntity;


/**
 * 好友请求Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Wed Aug 29 17:47:08 CST 2018
 */
@Service
public class FriendServiceImpl extends BaseServiceImpl<FriendEntity> implements FriendService {
    @Autowired
    private FriendDao friendDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private GroupUserDao groupUserDao;

/**内部方法**/


    /**外部方法实现**/


    /**
     * @Description: 添加好友时查找好友，判断是否已经是好友
     * @author hyh
     * @Date 10:13 2018/9/10 0010
     * @Param
     * @return
     */
    @Override
    public FriendVo findUser(Long userId, String phone) {
        FriendVo friendVo = new FriendVo();
        //先查询此人是否存在
        List<UserEntity> userEntityList = userDao.findByPhone(phone);
        if(userEntityList.isEmpty()){
            throw new RRException("用户不存在");
        }

        //查询是否已经是好友
        FriendEntity friendEntity = friendDao.isFriend(userId,userEntityList.get(0).getId());
        if(friendEntity!=null){
            friendVo.setFriend(true);
        }

        //设置vo
        friendVo.setId(userEntityList.get(0).getId());
        friendVo.setName(userEntityList.get(0).getName());
        friendVo.setAvatar(userEntityList.get(0).getAvatar());

        return friendVo;
    }


    /**
     * @Description: 查找好友
     * @author hyh
     * @Date 10:13 2018/9/10 0010
     * @Param
     * @return
     */
    @Override
    public List<FriendVo> findFriend(Long userId, String name) {

        List<FriendVo> list = new ArrayList();
        List<FriendEntity> friendEntityList = friendDao.getFriendbyName(userId,name);

        if(!friendEntityList.isEmpty()){
            for (FriendEntity friendEntity : friendEntityList) {
                FriendVo friendVo = new FriendVo();
                friendVo.setId(friendEntity.getToUserId());
                friendVo.setName(friendEntity.getToUserName());
                friendVo.setAvatar(friendEntity.getToUserAvatar());
                friendVo.setFriend(true);
                list.add(friendVo);
            }
        }

        return list;
    }

    @Override
    public int addFriend(Long fromUserId, Long toUserId) {
        //先查询申请人和接收人的信息
        UserEntity fromUser =  userDao.get(fromUserId);
        UserEntity toUser =  userDao.get(toUserId);
        //发送添加申请
        FriendEntity friendEntity = new FriendEntity(fromUser.getId(),fromUser.getName(),fromUser.getAvatar(),toUser.getId(),toUser.getName(),toUser.getAvatar(),new Date(),0,null);
        int save = friendDao.save(friendEntity);
        return save;
    }

    @Override
    public List<FriendVo> friendList(Long userId) {
        List<FriendVo> list = new ArrayList();
        List<FriendEntity> flist = friendDao.getFriendList(userId);
        for (FriendEntity friendEntity : flist) {
            FriendVo friendVo = new FriendVo();
            friendVo.setId(friendEntity.getToUserId());
            friendVo.setName(friendEntity.getToUserName());
            friendVo.setAvatar(friendEntity.getToUserAvatar());
            friendVo.setFriend(true);
            list.add(friendVo);
        }
        return list;
    }

    //好友申请列表
    @Override
    public List<FriendVo> getApplyList(Long userId) {
        List<FriendVo> list = new ArrayList();
        Map map = new HashMap();
        map.put("toUserId",userId);
        List<FriendEntity> friendEntityList = friendDao.queryList(map);
        if(!friendEntityList.isEmpty()){
            for (FriendEntity friendEntity : friendEntityList) {
                FriendVo friendVo = new FriendVo();
                friendVo.setId(friendEntity.getFromUserId());
                friendVo.setName(friendEntity.getFromUserName());
                friendVo.setAvatar(friendEntity.getFromUserAvatar());
                if(friendEntity.getIsFaild()==1){
                    friendVo.setFriend(true);
                }
                list.add(friendVo);
            }
        }

        return list;
    }

    //同意好友申请
    @Override
    public int agreeFriendApply(Long userId, Long friendId) {
        Map map = new HashMap();
        map.put("toUserId",userId);
        map.put("fromUserId",friendId);
        //先查询申请在不在
        List<FriendEntity> friendEntityList = friendDao.queryList(map);
        if(friendEntityList.isEmpty()){
            throw new RRException("申请不存在");
        }
        //更新申请信息
        FriendEntity friendEntity =  friendEntityList.get(0);
        friendEntity.setIsFaild(1);
        return friendDao.save(friendEntity);
    }


    //删除好友
    @Override
    public int deleteFriend(Long userId, Long friendId) {
        int result = 0;
        Map map = new HashMap();
        map.put("isFaild",1);
        map.put("fromUserId",userId);
        map.put("toUserId",friendId);
        List<FriendEntity> friendEntityList = friendDao.queryList(map);
        if(!friendEntityList.isEmpty()){
            result = friendDao.delete(friendEntityList.get(0).getId());
        }

        map.put("fromUserId",friendId);
        map.put("toUserId",userId);

        List<FriendEntity> friendEntityList1 = friendDao.queryList(map);
        if(!friendEntityList1.isEmpty()){
            result = friendDao.delete(friendEntityList1.get(0).getId());
        }

        return result;
    }

    @Override
    public boolean isFriend(Long userId, Long friendId) {
        //先验证是否是好友
        Map map = new HashMap();
        map.put("isFaild",1);
        map.put("fromUserId",userId);
        map.put("toUserId",friendId);
        List<FriendEntity> friendEntityList = friendDao.queryList(map);
        if(!friendEntityList.isEmpty()){
            return true;
        }else {
            map.put("toUserId",userId);
            map.put("fromUserId",friendId);
            List<FriendEntity> friendEntityList1 = friendDao.queryList(map);
            return !friendEntityList1.isEmpty();
        }
    }

    @Override
    public boolean isGroupUser(Long userId, Long groupId) {
        //先判断有没有加群
        GroupUserEntity group = groupUserDao.findByGroupIdAndUserId(userId,groupId);
        return group != null;
    }
}
